package com.jsits.v2x.common.util;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 反射工具 
 * @author zx
 *
 */
public class ReflectUtil {
	
	/**  
     * 获取obj对象fieldName的Field  
     * @param obj  
     * @param fieldName  
     * @return  
     */   
    public   static  Field getFieldByFieldName(Object obj, String fieldName) {  
        for  (Class<?> superClass = obj.getClass(); superClass != Object. class ; superClass = superClass  
                .getSuperclass()) 
        {  
            try  
            {  
                return  superClass.getDeclaredField(fieldName);  
            } catch  (NoSuchFieldException e)
            {  
            }  
        }  
        
        return   null ;  
    }  
  
    /**  
     * 获取obj对象fieldName的属性值  
     * @param obj  
     * @param fieldName  
     * @return  
     * @throws SecurityException  
     * @throws NoSuchFieldException  
     * @throws IllegalArgumentException  
     * @throws IllegalAccessException  
     */   
    public   static  Object getValueByFieldName(Object obj, String fieldName)  
            throws  SecurityException, NoSuchFieldException,  
            IllegalArgumentException, IllegalAccessException 
    {  
        Field field = getFieldByFieldName(obj, fieldName);  
        Object value = null ;  
        if (field != null )
        {  
            if  (field.isAccessible()) 
            {  
                value = field.get(obj);  
            } else  
            {  
                field.setAccessible(true );  
                value = field.get(obj);  
                field.setAccessible(false );  
            }  
        }  
        return  value;  
    }  
  
    /**  
     * 设置obj对象fieldName的属性值  
     * @param obj  
     * @param fieldName  
     * @param value  
     * @throws SecurityException  
     * @throws NoSuchFieldException  
     * @throws IllegalArgumentException  
     * @throws IllegalAccessException  
     */   
    public   static   void  setValueByFieldName(Object obj, String fieldName,  
            Object value) throws  SecurityException, NoSuchFieldException,  
            IllegalArgumentException, IllegalAccessException 
    {  
        Field field = getFieldByFieldName(obj,fieldName);  
        if  (field.isAccessible()) 
        {  
            field.set(obj, value);  
        } else  
        {  
            field.setAccessible(true );  
            field.set(obj, value);  
            field.setAccessible(false );  
        }  
    }  
    
    /**
     * 将数据库返回的值转换并设置到对应属性
     * @param resultObject
     * @param fieldName
     * @param cols_value
     * @throws NumberFormatException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws ParseException
     */
    public static void setValueForJDBC(Object resultObject, String fieldName,  
            Object cols_value) throws NumberFormatException, IllegalArgumentException, IllegalAccessException, ParseException
     {
    	
    	Field field = ReflectUtil.getFieldByFieldName(resultObject, fieldName);  
    	
    	if(field != null)
    	{
            field.setAccessible(true); //打开javabean的访问权限  
            
            if(cols_value != null)
            {
				if (field.getType().getSimpleName().equals("Integer")) 
				{
					field.set(resultObject, Integer.parseInt(cols_value.toString()));
					
				} else if (field.getType().getSimpleName().equals("int")) 
				{
					field.set(resultObject, Integer.parseInt(cols_value.toString()));

				} else if (field.getType().getSimpleName().equals("String")) 
				{
					field.set(resultObject, cols_value.toString());

				} else if (field.getType().getSimpleName().equals("double")) 
				{
					field.set(resultObject, Double.parseDouble(cols_value.toString()));
					
				} else if (field.getType().getSimpleName().equals("Double")) 
				{
					field.set(resultObject, Double.parseDouble(cols_value.toString()));
					
				} else if (field.getType().getSimpleName().equals("Date")) 
				{
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					field.set(resultObject, sdf.parse(cols_value.toString()));
					
				} else if (field.getType().getSimpleName().equals("float")) 
				{
					field.set(resultObject, Float.parseFloat(cols_value.toString()));
					
				} else if (field.getType().getSimpleName().equals("Float")) 
				{
					field.set(resultObject, Float.parseFloat(cols_value.toString()));
					
				}else if (field.getType().getSimpleName().equals("Clob"))
				{
					field.set(resultObject, cols_value);
					
				}else if (field.getType().getSimpleName().equals("Blob"))
				{
					field.set(resultObject, cols_value);
				}else if (field.getType().getSimpleName().equals("Text"))
				{
					field.set(resultObject, cols_value.toString());
				}
            }
            
            field.setAccessible(false);
    	}
    }

}
